import comm from './core';

function transform (data) {
  const {
    code,
    name,
    bigImageUrl,
    introduction,
    arrangeTreeElements = []
  } = data;
  return {
    code,
    title: name,
    intro: introduction,
    picUrl: comm.formatZoomPic(bigImageUrl),
    list: arrangeTreeElements.map((item) => { return transformList(item); }) || []
  };
}

function transformList (item) {
  const {
    itemName,
    picUrl,
    videoSource,
    videoType,
    subtitle,
    linkId,
    duration,
    programType,
    statQueryDto
  } = item;
  if (programType === 'arrange') {
    return {
      code: linkId,
      title: itemName,
      picUrl: comm.formatZoomPic(picUrl),
      childrenCount: item.detailCount,
      introduction: item.introduction,
      programType
    };
  }
  return {
    code: linkId,
    title: itemName,
    picUrl: comm.formatZoomPic(picUrl),
    subtitle,
    videoSource,
    programType,
    videoType,
    duration: comm.formatSecond(duration),
    playCount: comm.dealNumber(statQueryDto.playCount)
  };
}

export function get () {
  const params = comm.requestParams();
  const {
    origin,
    code,
    status
  } = params;

  if (!status || status !== 1) {
    return {};
  }
  const path = `arrangeTree/findTreeNodeCode/${code}?v=1&containArrange=1`;
  return comm.request({ origin, path }).then(({ data }) => {
    if (data) {
      const response = transform(data);
      return response;
    }
  });
}

export default {
  get
};
